﻿#include "hoteldetailsheader.h"
#include "ui_hoteldetailsheader.h"
#include <QMessageBox>

HotelDetailsHeader::HotelDetailsHeader(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HotelDetailsHeader)
{
    ui->setupUi(this);

    m_page = 1;
    m_pageCount = 1;
    m_save = false;
    ui->pb_pre->setEnabled(false);
    ui->pb_next->setEnabled(false);
    ui->pb_image1->close();  ui->pb_image2->close();
    ui->pb_image3->close();  ui->pb_image4->close();
}

HotelDetailsHeader::~HotelDetailsHeader()
{
    delete ui;
}

void HotelDetailsHeader::initWindowsUnits()
{
    ui->lb_hotelName->setText(m_hotelInfo.getName());
    ui->lb_location->setText(m_hotelInfo.getLocation());
    ui->lb_star->setText(m_hotelInfo.getStar());
    ui->lb_phone->setText("电话：" + m_hotelInfo.getPhone());
    ui->lb_price->setText(QString::number(m_hotelInfo.getLowPrice()));

    QString commentCount = QString::number(m_hotelInfo.getCommentCount());
    ui->pb_watchComment->setText("查看"+commentCount+"条评论");

    QString mark = m_hotelInfo.getMark();
    ui->lb_mark->setText(mark + "分");
    if (mark.toDouble() > 4.5)
    {
        ui->lb_markWord->setText("很好");
    }
    else if (mark.toDouble() < 4.0)
    {
        ui->lb_markWord->setText("不错");
    }
    else{
        ui->lb_markWord->setText(" 好");
    }

    QString decoratedTime = m_hotelInfo.getDecoratedTime() + "年装修";
    QString openedTime = m_hotelInfo.getOpenedTime() + "年开业";
    ui->lb_openTime->setText(decoratedTime + " " + openedTime);
    if (m_hotelInfo.getIsSave())
    {
        m_save = true;
        ui->pb_save->setStyleSheet("color: rgb(255, 85, 0); background-color: transparent;");
    }else
    {
        m_save = false;
        ui->pb_save->setStyleSheet("color: rgb(0, 0, 0); background-color: transparent;");
    }

    QStringList server = m_hotelInfo.getServer().split(",");
    if (server.size() == 1)
    {
        if (server.at(0).isEmpty())
        {
            return;
        }
    }
    switch (server.size())
    {
        default:
        case 4:
            ui->lb_server4->setText(server.at(3));
            ui->lb_icon4->setStyleSheet(GlobalVars::g_serverStyleSheet[server.at(3)]);
        case 3:
            ui->lb_server3->setText(server.at(2));
            ui->lb_icon3->setStyleSheet(GlobalVars::g_serverStyleSheet[server.at(2)]);
        case 2:
            ui->lb_server2->setText(server.at(1));
            ui->lb_icon2->setStyleSheet(GlobalVars::g_serverStyleSheet[server.at(1)]);
        case 1:
            ui->lb_server1->setText(server.at(0));
            ui->lb_icon1->setStyleSheet(GlobalVars::g_serverStyleSheet[server.at(0)]);
        case 0: break;
    }
}

void HotelDetailsHeader::setHotelInfo(const HotelInfo &hotelInfo)
{
    m_hotelInfo = hotelInfo;
    initWindowsUnits();
}

void HotelDetailsHeader::setImage(const QList<QImage>& imageList)
{
    m_imageList = imageList;
    m_imageList.removeFirst();
    m_pageCount = ceil(m_imageList.size() / 4.0);
    if (m_pageCount <= 1)
    {
        m_pageCount = 1;
    }
    else
    {
        ui->pb_next->setEnabled(true);
    }
    qDebug() << "m_page" << m_page;
    qDebug() << "m_imageList.size = " << m_imageList.size();

    switch (imageList.size())
    {
        case 0: return;break;
        default:
        case 4:
            ui->lb_image4->setPixmap(QPixmap::fromImage( m_imageList.at(3).scaled(120,100)));
            ui->pb_image4->show();
        case 3:
            ui->lb_image3->setPixmap(QPixmap::fromImage( m_imageList.at(2).scaled(117,100)));
            ui->pb_image3->show();
        case 2:
            ui->lb_image2->setPixmap(QPixmap::fromImage( m_imageList.at(1).scaled(117,100)));
            ui->pb_image2->show();
        case 1:
        {
            ui->lb_imageB->setPixmap(QPixmap::fromImage( m_imageList.at(0).scaled(480,200)));
            ui->lb_image1->setPixmap(QPixmap::fromImage( m_imageList.at(0).scaled(117,100)));
            ui->pb_image1->show();
        } break;
    }
}

void HotelDetailsHeader::setSmallImage()
{
    qDebug() << "result = " << m_imageList.size() - (m_page-1)*4;
    qDebug() << "m_page = " << m_page << m_pageCount;
    if (m_page < m_pageCount)
    {
        ui->pb_image1->show();  ui->pb_image2->show();
        ui->pb_image3->show();  ui->pb_image4->show();
        ui->lb_image1->show();   ui->lb_image2->show();
        ui->lb_image3->show();   ui->lb_image4->show();
        ui->lb_image4->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+3).scaled(117,100)));
        ui->lb_image3->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+2).scaled(117,100)));
        ui->lb_image2->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+1).scaled(117,100)));
        ui->lb_image1->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4).scaled(120,100)));
    }
    else
    {
        switch (m_imageList.size()%4)
        {
            case 0:
            {
                ui->pb_image1->show();  ui->pb_image2->show();
                ui->pb_image3->show();  ui->pb_image4->show();
                ui->lb_image1->show();   ui->lb_image2->show();
                ui->lb_image3->show();   ui->lb_image4->show();
                ui->lb_image4->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+3).scaled(120,100)));
                ui->lb_image3->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+2).scaled(117,100)));
                ui->lb_image2->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+1).scaled(117,100)));
                ui->lb_image1->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4).scaled(117,100)));
            } break;

            case 3:
            {
                ui->pb_image1->show();  ui->pb_image2->show();
                ui->pb_image3->show();  ui->pb_image4->close();
                ui->lb_image1->show();   ui->lb_image2->show();
                ui->lb_image3->show();   ui->lb_image4->close();
                ui->lb_image3->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+2).scaled(117,100)));
                ui->lb_image2->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+1).scaled(117,100)));
                ui->lb_image1->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4).scaled(117,100)));
            } break;

            case 2:
            {
                ui->pb_image1->show();  ui->pb_image2->show();
                ui->pb_image3->close();  ui->pb_image4->close();
                ui->lb_image1->show();   ui->lb_image2->show();
                ui->lb_image3->close();   ui->lb_image4->close();
                ui->lb_image2->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+1).scaled(117,100)));
                ui->lb_image1->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4).scaled(117,100)));
            } break;

            case 1:
            {
                ui->pb_image1->show();  ui->pb_image2->close();
                ui->pb_image3->close();  ui->pb_image4->close();
                ui->lb_image1->show();   ui->lb_image2->close();
                ui->lb_image3->close();   ui->lb_image4->close();
                ui->lb_image1->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4).scaled(117,100)));
            } break;
            default: break;
        }
     }
}

void HotelDetailsHeader::on_pb_pre_clicked()
{
    m_page--;
    ui->pb_next->setEnabled(true);
    qDebug() << "pb_pre      page =" << m_page;

    if (m_page == 1)
    {
        ui->pb_pre->setEnabled(false);
    }
    setSmallImage();
}

void HotelDetailsHeader::on_pb_next_clicked()
{
    ui->pb_pre->setEnabled(true);
    m_page++;
    qDebug() << "pb_next      page =" << m_page;

    if (m_page == m_pageCount)
    {
        ui->pb_next->setEnabled(false);
    }
    setSmallImage();
}

void HotelDetailsHeader::on_pb_image1_clicked()
{
    ui->lb_imageB->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4).scaled(480,200)));
}

void HotelDetailsHeader::on_pb_image2_clicked()
{
    ui->lb_imageB->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+1).scaled(480,200)));
}

void HotelDetailsHeader::on_pb_image3_clicked()
{
    ui->lb_imageB->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+2).scaled(480,200)));
}

void HotelDetailsHeader::on_pb_image4_clicked()
{
    ui->lb_imageB->setPixmap(QPixmap::fromImage(m_imageList.at((m_page-1)*4+3).scaled(480,200)));
}

void HotelDetailsHeader::setSubmitSaveResult(bool result, const QString& msg)
{
    if (result)
    {
        if (m_save)
        {
            ui->pb_save->setStyleSheet("color: rgb(255, 85, 0); background-color: transparent;");
        }
        else
        {
            ui->pb_save->setStyleSheet("color: rgb(0, 0, 0); background-color: transparent;");
        }
    }
    else
    {
        QMessageBox::warning(this,"错误", msg, QMessageBox::Yes, QMessageBox::Yes);
    }
}

void HotelDetailsHeader::on_pb_save_clicked()
{
    if (!m_save)
    {
        m_save = true;
    }
    else
    {
        m_save = false;
    }

    emit signalSubmitSave(m_save);
}

void HotelDetailsHeader::on_pb_watchComment_clicked()
{
    emit signalJumpToComment();
}
